
<template>
<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"  style="width:40%;margin:0 auto;vertical-align: middle;vertical-align:middle;"  class="demo-ruleForm">
  <el-form-item label="用户名" prop="name">
    <el-input v-model.number="ruleForm.name"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="确认密码" prop="checkPass">
    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
  </el-form-item>
  
  <el-form-item   style="margin:50px auto 50px auto ;width:fit-content">
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
    <el-button type="primary" @click="goLogin" style="background:none;color:black;border:none">去登录</el-button>
  </el-form-item>
</el-form>
</template>

<script>
  export default {
    data() {
      var checkName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('用户名不能为空'));
        }
        else{
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
          name: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          name: [
            { validator: checkName, trigger: 'blur' }
          ]
        }
      };
    },
    
    methods: {
//接口：发送表单

      submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios({
                            url: '',
                            methods: 'get',
                            data: {
                                ...this.resetForm
                            }
                        }).then(res => {
                           console.log(res);
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },goLogin(){
         this.$router.replace('/')
       }
    }
  }
</script>